<template>
  <div class="app-container">
    <el-form :model="form" ref="form" label-width="100px" v-loading="formLoading" :rules="rules">
      <el-form-item label="任务标题: " prop="name" required>
        <el-input v-model="form.name" class="input-name"/>
      </el-form-item>
      <el-form-item label="发布班级: " prop="to_class" required>
        <el-select v-model="form.to_class" placeholder="选择班级">
          <el-option v-for="item in class_list" :key="item.id" :value="item.id"
                     :label="item.name"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="考试类型: " prop="task_type" required>
        <el-select v-model="form.task_type" placeholder="选择考试类型">
          <el-option v-for="item in task_type_list" :key="item.id" :value="item.id"
                     :label="item.name"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间: " prop="start_time" required>
        <el-date-picker
            v-model="form.start_time"
            type="datetime"
            placeholder="选择考试开始时间日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="考试分钟: " prop="duration">
        <el-input-number v-model="form.duration" :min="0" :max="10000" :step="10" label="考试时长分钟"></el-input-number>
      </el-form-item>
      <el-form-item label="考试试卷: " prop="paper_dict">
        <el-descriptions style="margin-top: 10px;" title="" :column="3" size="500" border>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-star-on"></i>
              试卷名称
            </template>
            {{ form.paper_dict ? form.paper_dict.name : '' }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-menu"></i>
              学科
            </template>
            {{ form.paper_dict ? form.paper_dict.subject_name : '' }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-discount"></i>
              试卷总分
            </template>
            <el-tag size="small" type="danger">{{ form.paper_dict ? form.paper_dict.paper_score : 0 }}</el-tag>
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-watch"></i>
              创建时间
            </template>
            {{ form.paper_dict ? form.paper_dict.time : '' }}
          </el-descriptions-item>
        </el-descriptions>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
        <el-button type="success" @click="selectPaper">选择试卷</el-button>
      </el-form-item>
    </el-form>
    <el-dialog :visible.sync="PaperPage.showDialog" width="70%">
      <el-form :inline="true">
        <el-form-item>
          <el-input
              v-model="PaperPage.search"
              placeholder="输入试卷标题搜索"
              clearable
              @change="getPaperList">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getPaperList" class="el-icon-search" type="primary"></el-button>
        </el-form-item>
        <el-form-item>
          <template>
            <el-select v-model="PaperPage.subject" clearable placeholder="按学科分类" @change="getPaperList">
              <el-option
                  v-for="item in PaperPage.subject_list"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
      </el-form>

      <el-table
          ref="singleTable"
          :data="PaperPage.tableData"
          tooltip-effect="dark"
          highlight-current-row
          style="width: 100%"
          border
          stripe
          @selection-change="handleSelectionChange">
        <el-table-column
            type="selection"
            width="40">
        </el-table-column>
        <el-table-column
            prop="id"
            label="id"
            width="70">
        </el-table-column>
        <el-table-column
            prop="name"
            label="试卷名称"
            width="630">
          <template slot-scope="scope">
            <span v-html="scope.row.name"></span>
          </template>
        </el-table-column>
        <el-table-column
            prop="subject_name"
            label="学科"
            width="140">
        </el-table-column>
        <el-table-column
            prop="paper_score"
            label="分数"
            show-overflow-tooltip>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          layout="total, sizes, prev, pager, next, jumper"
          :page-sizes="[5, 10, 20, 50, 100]"
          :current-page="PaperPage.current"
          :page-size="PaperPage.size"
          :total="PaperPage.total">
      </el-pagination>
      <div class="select-btn">
        <el-button @click="PaperPage.showDialog = false">取 消</el-button>
        <el-button type="primary" @click="confirmQuestionSelect">确定</el-button>
      </div>
    </el-dialog>
    <div style="text-align: center; padding-bottom: 10px;">&COPY;copyleft@海饼干</div>
  </div>
</template>

<script>


export default {
  components: {},
  data() {
    return {
      form: {
        id: null,
        name: '',
        to_class: null,
        task_type: null,
        start_time: null,
        duration: null,
        status: 0,
        paper_dict: {},
      },
      class_list: [],
      task_type_list: [
        {id: 0, name: '定时长考试'},
        {id: 1, name: '不定长考试'},
      ],
      formLoading: false,
      rules: {
        to_class: [
          {required: true, message: '请选择要考试的班级', trigger: 'change'}
        ],
        name: [
          {required: true, message: '请输入考试任务标题', trigger: 'blur'}
        ],
        task_type: [
          {required: true, message: '请选择考试类型', trigger: 'blur'}
        ],
        start_time: [
          {required: true, message: '请选择考试开始时间', trigger: 'blur'}
        ],
        duration: [
          {required: true, message: '请选择考试时长', trigger: 'blur'}
        ],
        paper_dict: [
          {required: true, message: '请选择考试的试卷', trigger: 'blur'}
        ],
      },
      PaperPage: {
        listLoading: true,
        showDialog: false,
        subject: null,
        search: '',
        total: 0,
        size: 10,
        current: 1,
        subject_list: [],
        tableData: [],
      },
    }
  },
  created() {
    let id = this.$route.query.id
    this.getClass_list()
    this.getSubjects()
    if (id) {
      this.getTask(id)
    }
  },
  methods: {
    getSubjects() {
      this.$http({
        url: this.$settings.BASE_URL + '/paper/subject_list/',
        method: 'get',
      }).then(res => {
        this.PaperPage.subject_list = res.data.result.results
      })
    },
    getTask(id) {
      this.$http({
        url: this.$settings.BASE_URL + `/paper/task_list/${id}/`,
        method: 'get',
      }).then(res => {
        if (res.data.code === 20000) {
          this.form = res.data.result
          this.$http({
            url: this.$settings.BASE_URL + `/paper/paper_List/${res.data.result.paper}`,
            method: 'get',
          }).then(response => {
            this.form.paper_dict = response.data.result
            this.getClass_list()
          })
        } else {
          this.$message({
            type: 'error',
            message: '系统错误,请稍后重试',
            duration: 3000,
          })
        }
      })
    },
    getClass_list() {
      this.$http({
        url: this.$settings.BASE_URL + '/user/student_class/',
        method: 'get',
      }).then(res => {
        if (res.data.code === 20000) {
          this.class_list = res.data.result.results
        } else {
          this.$message({
            type: 'error',
            message: res.data.msg,
            duration: 3000,
          })
        }
      })
    },
    handleSizeChange(val) {
      this.PaperPage.size = val
      this.getPaperList()
    },
    handleCurrentChange(val) {
      this.PaperPage.current = val
      this.getPaperList()
    },
    submitForm() {
      console.log(this.form)
      this.$http({
        url: this.$settings.BASE_URL + '/paper/task_list/',
        method: 'post',
        data: this.form,
      }).then(res => {
        if (res.data.code === 20000) {
          this.$message({
            type: 'success',
            message: res.data.msg,
            duration: 3000,
            onClose: () => {
              this.resetForm()
            }
          })
        } else {
          this.$message({
            type: 'error',
            message: '提交失败,请检查信息是否完整,考试开始时间是否已过期',
            duration: 3000,
          })
        }
      })
    },
    selectPaper() {
      this.PaperPage.showDialog = true
      this.getPaperList()
    },
    confirmQuestionSelect() {
      this.PaperPage.showDialog = false
    },
    getPaperList() {
      this.PaperPage.listLoading = true
      this.$http({
        url: this.$settings.BASE_URL + '/paper/paper_List/',
        method: 'get',
        params: {
          page: this.PaperPage.current,
          size: this.PaperPage.size,
          search: this.PaperPage.search,
          subject: this.PaperPage.subject,
        },
      }).then(res => {
        if (res.data.code === 20000) {
          this.PaperPage.tableData = res.data.result.results
          this.PaperPage.total = res.data.result.count
          this.PaperPage.listLoading = false
        } else {
          this.$message({
            type: 'warning',
            message: res.data.msg,
            duration: 3000,
          })
        }
      })
    },
    handleSelectionChange(val) {
      this.form.paper_dict = val[0]
    },
    resetForm() {
      this.$refs['form'].resetFields()
      this.form = {
        id: null,
        name: '',
        to_class: null,
        task_type: null,
        start_time: null,
        duration: null,
        status: 0,
        paper_dict: null,
      }
    },
  },
  computed: {}
}
</script>

<style scoped>
.input-name {
  width: 650px;
}

.select-btn {
  position: relative;
  left: 442px;
  width: 200px;
}
</style>
